//
//  HMQuickQuestion.swift
//  HealthMonitoring
//
//  Created by 珠海正杰 on 2018/9/17.
//  Copyright © 2018年 YTYanK. All rights reserved.
//

import UIKit
import SwiftyJSON

class HMQuickQuestion: HMViewController {
    var cellHeight: CGFloat = 0
    
    
    private lazy var table: UITableView = {
       let _table = UITableView()
        _table.separatorStyle = UITableViewCellSeparatorStyle.none
        return _table
    }()
    
    override func setupView() {
       view.backgroundColor = configure.shareConfigure.HMColor3
       view.addSubview(table)
        
        table.delegate = self
        table.dataSource = self
        table.register(HMProposalCell.self, forCellReuseIdentifier: "cell")
        table.topAlign(view: view, predicate: "0")
        table.leadingAlign(view: view, predicate: "0")
        table.width(view: view)
        table.bottomAlign(view: view, predicate: "-44")
    }
    
}

extension HMQuickQuestion: UITableViewDelegate, UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
       let header = UIView()
        header.backgroundColor = configure.shareConfigure.HMColor2
        
        // 先用输入框
        let search = UITextField()
        header.addSubview(search)
        search.topAlign(view: header)
        search.leadingAlign(view: header)
        search.trailingAlign(view: header)
        search.bottomAlign(view: header)
//        search.height(view: header, predicate: "*0.8")
        search.placeholder = "请输入搜索内容🔍"
        search.backgroundColor = UIColor.white
        search.layer.cornerRadius = 8
        search.textAlignment = .center
//        search.text
        return header
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 25
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return self.cellHeight
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // 生活建议部分
        let arry =  ["title": "生活建议:", "ary": ["123456", "456456465"]] as [String : Any]
        //药单部分
        let dic  = [["drugsName":"药名", "period": "疗程"],["drugsName":"药名", "period": "疗程"],["drugsName":"药名", "period": "疗程"],["drugsName":"药名", "period": "疗程"]]
        // 标题部分
        let dat: JSON = ["value" : "编辑生活建议", "describe" : "心脏病", "adviceLife" : arry]

        if indexPath.row == 24 {
            let cell = HMQuestionAppend.cell(json: nil, tableView: tableView, cellForRowAt: indexPath)
            cell.setJson(json: ["icon": "add", "text": "点击添加药物"])
            self.cellHeight = cell.cellHeight
            return cell
            
        }else {
            let cell = HMQuestionCell.cell(json: dat, tableView: tableView, cellForRowAt: indexPath)
            self.cellHeight = (cell.cellHeight)
            return cell
        }


    }
    
}
